package com.base.chat.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.base.chat.R;
import com.base.chat.bean.SearchFriendsBean;
import com.base.chat.databinding.FragmentSearchFriendsBinding;
import com.base.chat.presenter.SearchFriendsListPresenter;
import com.base.chat.util.ChatStatEntity;
import com.base.chat.view.SearchFriendsListView;
import com.base.commonlib.base.CBaseMvpFragment;
import com.base.commonlib.base.adapter.BaseAdapter;
import com.base.commonlib.base.adapter.BaseHolder;
import com.base.commonlib.bean.StatEntity;
import com.base.commonlib.resh.footer.ClassicsFooter;
import com.base.commonlib.resh.header.ClassicsHeader;
import com.base.commonlib.utils.GlideUtil;
import com.base.commonlib.utils.RxBus2;
import com.base.commonlib.utils.ToastUtil;
import com.base.commonlib.widget.OvalImageView;
import com.base.commonlib.widget.RecyclerViewSpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class SearchFriendsListFragment extends CBaseMvpFragment<SearchFriendsListPresenter>implements SearchFriendsListView {

    private FragmentSearchFriendsBinding mBinding;
    private SearchFriendsBean searchFriendsBean;
    private BaseAdapter<SearchFriendsBean.Rows> baseAdapter;
    private List<SearchFriendsBean.Rows> mData=new ArrayList<>();
    private Disposable disposable;

    @Override
    public void init() {
        mBinding = (FragmentSearchFriendsBinding) mRootBinding;
        mBinding.smartRefreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        mBinding.smartRefreshLayout.setHeaderHeight(60);
        mBinding.smartRefreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
        mPresenter =new SearchFriendsListPresenter(this,getActivity());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (disposable != null) {
            disposable.dispose();
        }
    }

    @Override
    public void start() {
        disposable = RxBus2.getInstance().doSubscribe(ChatStatEntity.class, new Consumer<ChatStatEntity>() {
            @Override
            public void accept(ChatStatEntity bean) throws Exception {
                if (bean.getState() == 0X0000010) {
                    if (mPresenter!=null){
                        mPresenter.getList(bean.getData());
                    }
                }
            }
        });
        baseAdapter = new BaseAdapter<SearchFriendsBean.Rows>(mData, mContext) {
            @Override
            public void convert(BaseHolder holder, final SearchFriendsBean.Rows bean, boolean isHeader, boolean isBottom) {
                TextView it_name = holder.getView(R.id.it_name);
                TextView it_add = holder.getView(R.id.it_add);
                OvalImageView it_image = holder.getView(R.id.it_image);
                GlideUtil.load(mContext,bean.getHeadImageUrl(),it_image);
                it_name.setText(bean.getUserName());
                it_add.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPresenter.addFriend(bean.getUserId(),"USER");
                    }
                });

            }
        };

        baseAdapter.putNormalType(R.layout.item_search_friends);
        baseAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseHolder holder, int dataPosition, boolean isHeader, boolean isBottom) {
                SearchFriendsBean.Rows rows=mData.get(dataPosition);

                //ARouter.getInstance().build("/lpark/parkdetail").withParcelable("mPark",illegalPark).withInt("mType",1).navigation();
            }
        });
        mBinding.rv.setAdapter(baseAdapter);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mBinding.rv.setLayoutManager(linearLayoutManager);
        mBinding.rv.addItemDecoration(new RecyclerViewSpacesItemDecoration
                (10, 10, 10, 10));
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_search_friends;
    }


    @Override
    public void returnNoData() {

    }

    @Override
    public void returnList(SearchFriendsBean bean) {
        //mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishRefresh();
        mBinding.smartRefreshLayout.setLoadmoreFinished(false);
        mData.clear();
        mData.addAll(bean.getRows());
        baseAdapter.notifyDataSetChanged();
    }

    @Override
    public void returnMoreList(SearchFriendsBean bean) {
       // mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishLoadmore();
        mBinding.smartRefreshLayout.setLoadmoreFinished(false);
        mData.addAll(bean
                .getRows());
        if (baseAdapter != null) {
            baseAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void returnNoMoreData() {

    }

    int page =1;

    @Override
    public void returnError() {
        //mBinding.empty.setVisibility(View.GONE);
        if (page == 1) {
            mBinding.smartRefreshLayout.finishRefresh();
        } else {
            mBinding.smartRefreshLayout.finishLoadmore();
        }
    }

    @Override
    public void returnAddFriendSuccess(String msg) {
        ToastUtil.show(msg);
    }
}
